home *** CD-ROM | disk | FTP | other *** search
- /*
- * A potpourri of handy little utilities.
- *
- * Michael Hawley
- * MIT Media Laboratory
- * 20 Ames St
- * Cambridge, MA 02139
- * mike@media-lab.mit.edu
- * Copyright (c) MIT Media Laboratory 1991
- */
-
- #include <sys/stat.h>
- #include <libc.h>
- #include <sys/stat.h>
- #include <regex.h>
- #include "util.h"
- #include "state.h"
-
- char *av0 = "Weather.app";
-
- void error(char *fmt, ...) {
- va_list ap;
- va_start(ap, fmt);
- vprintf(fmt, ap), printf("\n");
- va_end(ap);
- }
-
- int Verbose=0; /* make it 1 to send copious diganostics to the console */
-
- void debug(char *fmt, ...) { /* printf an error msg */
- va_list ap;
- va_start(ap, fmt);
- if (Verbose) {
- vprintf(fmt, ap), printf("\n");
- }
- va_end(ap);
- }
-
- int System(char *fmt, ...)
- {
- va_list ap;
- int i=0;
- char t[2048];
-
- va_start(ap, fmt);
- vsprintf(t, fmt, ap);
- debug("!%s",t);
- if (i = system(t)) error("%s: failed! %s",av0,t);
- va_end(ap);
- return !i;
- }
-
- char *save(char *s) { /* save a copy of 's' and return the pointer */
- char *t = (char *)malloc(strlen(s)+1);
- if (t) strcpy(t,s);
- return t;
- }
-
- int blank(char *s) { /* true if 's' is blank */
- while (*s == ' ' || *s=='\t' || *s == '\n') ++s;
- return !*s;
- }
-
- void stripcomment(char *s) { /* truncate 's' at a comment character ('#') */
- char *p = index(s,'#');
- if (p && (p==s || p[-1] != '\\')) *p = '\0';
- }
-
- void stripnl(char *s) { /* remove trailing \n and space from 's' */
- char *p = s + strlen(s)-1;
- while (p>s && (*p=='\n' || *p == ' ')) *p-- = '\0';
- }
-
- char *skipsp(char *s) {
- while (*s==' ' || *s=='\t' || *s == '\n') ++s;
- return s;
- }
-
- void squishblank(char *s) {
- char *t=s;
- while (*t = *s){
- if (t>s && (*t==' ' || *t=='\t') && (t[-1]==' '||t[-1]=='\t'))
- ++s;
- else
- ++t, ++s;
- }
- }
-
- void squishwhite(char *s) {
- char *t=s;
- while (*t = *s){
- if (t>s && (*t==' ' || *t=='\t' || *t=='\n') &&
- (t[-1]==' '||t[-1]=='\t'||t[-1]=='\n'))
- ++s;
- else
- ++t, ++s;
- }
- }
-
- char *
- prefix(char *s, char *t) { /* true if 't' is a prefix of 's' */
- int sl = strlen(s);
- t = skipsp(t);
- return (*t == *s && strncmp(s,t,sl)==0)? skipsp(t+sl) : (char *)0;
- }
-
- int suffix(char *s, char *t) { /* true if 't' is a suffix of 's' */
- s = rindex(s,'.');
- return s? strcmp(s,t)==0 : 0;
- }
-
- char *
- strindex(char *s, char *t) { /* return ptr to first match of 't' in 's' */
- int n = strlen(t);
-
- if (s)
- while (*s)
- if (!strncmp(s, t, n))
- return s;
- else
- s++;
- return (char *)0;
- }
-
- void sub(char *s, char a, char b) { /* in 's', s/a/b/g */
- while (*s){
- if (*s==a) *s=b;
- s++;
- }
- }
-
- void substr(char *s, char *a, char *b) { /* like 'sub', but with strings */
- char q[8192];
- char *p = s;
- int n = strlen(a);
- for (;*p;p++){
- if (*p == *a && strncmp(p,a,n)==0){
- strcpy(q,p+n);
- strcpy(p,b);
- strcpy(p+strlen(b),q);
- p += strlen(b)-1;
- }
- }
- }
-
- void stot(char *s, char **t, char c) /* split 's' into a table 't' */
- {
- char *p;
- while (p = index(s,c)){
- *p++ = '\0';
- *t++ = s;
- s = p;
- }
- *t++ = s;
- *t = (char *)0;
- }
-
-
- int fMode(char *f) { /* return mode bits */
- struct stat b;
- return stat(f,&b)? 0 : b.st_mode;
- }
-
- int fSize(char *f) { /* return mode bits */
- struct stat b;
- return stat(f,&b)? 0 : b.st_size;
- }
-
- int fTime(char *f) { /* mod time of file 'f' */
- struct stat b;
- return stat(f,&b)? 0 : b.st_mtime;
- }
-
- int fDirectory(char *f) { /* true if file 'f' is a directory */
- struct stat b;
- return stat(f,&b)? 0 : ((b.st_mode & S_IFDIR) == S_IFDIR);
- }
-
- int fLink(char *f) { /* true if file 'f' is a symbolic link */
- struct stat b;
- return lstat(f,&b)? 0 : ((b.st_mode & S_IFLNK) == S_IFLNK);
- }
-
- int
- mkdirs(char *s, int mode) { /* ensure that all the directories in 's' exist */
- char t[4096], *p = t;
-
- strcpy(t,s);
- if (*p=='/') ++p;
- while (p = index(p,'/')) {
- *p = '\0';
- if (access(t,0)){
- if (mkdir(t,mode))
- return 0;
- /* fixmod(t); */
- }
- *p++ = '/';
- }
- if (access(t,0) && mkdir(t,mode)) return 0;
- /* fixmod(t); */
- debug("mkdir %s",t);
- return 1;
- }
-
-
-
- int match(char *s, char *expr)
- /*
- * true if 's' matches regular expression 'expr'
- * (a simple regex, grep-style, *not* egrep)
- */
- {
- static char p[256]="", *q=(char *)0;
- static struct regex *r = (struct regex *)0;
- if (q != expr){
- if (strcmp(p,expr)){
- if (r) free(r);
- if (r=re_compile(expr,0))
- ;
- else return 0;
- strcpy(p,expr);
- }
- q = expr;
- }
- return r? re_match(s,r)==1 : 0;
- }
-